<?php
namespace admin\module\member;
use inc\BaseAjax\BaseAjax;
require('../../../global.php');

class ajax extends BaseAjax
{
    
    /**
     * 列出所有的后台员工
     * @Author : bapijun
     * param search 搜索的名称 sort 排序方向 asc正向排序 desc逆向排序
     * echo 根据搜索参数返回符合条件的部门或者职位数组
     */
    protected function listTable()
    {
        global $db;
        if (checkVar($_GET['search'])) {
            $where['OR']['nickname[~]'] = $_GET['search'];
            $where['OR']['realname[~]'] = $_GET['search'];
            $where['OR']['mobile[~]'] = $_GET['search'];
        }
        if (checkVar($_GET['sort']) && checkVar($_GET['order'])) {
            $where['ORDER'][$_GET['sort']] = strtoupper($_GET['order']);
        } else {
            $where['ORDER']['tbid'] = 'DESC';
        }
        $echo['total'] = $db->count('tb_member', '*', $where);
        $where['LIMIT'] = array($_GET['offset'], $_GET['limit']);
        $echo['rows'] = array();
        $row = $db->select('tb_member', '*', $where);
        foreach ($row as $value) {
            $img = 'dofiles/' .$value['tbid'] . '/head/original_head.jpg';

            $avatar = file_exists($img) ? $img : 'admin/img/avatar.jpg';
            $tmp['preview'] = '<img src="'.SITEROOTURL.$avatar.'" width="64" height="64" />';
            $tmp['nickname'] = $value['nickname'];
            $tmp['realname'] = $value['realname'];
            $tmp['mobile'] = $value['mobile'];
            $tmp['createtime'] = $value['createtime'];
            $whereLoginTimeWhere['AND']['member_id'] = $value['tbid'];
            $whereLoginTimeWhere['ORDER']['tbid'] = 'DESC';
            $tmp['last_login_time'] = $db->get('login_log', 'dt', $whereLoginTimeWhere);
            $tmp['last_login_time'] = ($tmp['last_login_time'])?$tmp['last_login_time']:'';
            if(checkAdminAuthorize('member.edit')){
                $tmp['do'] = ' <a href="javascript:;" class="btn btn-primary btn-xs edit" data-id="'.$value['tbid'].'">查看详情页</a> ';
               /*  if($value['tbid'] != session('admin_id')){
                    $tmp['do'] .= ' <button type="button" class="btn btn-danger btn-outline btn-xs remove" data-id="'.$value['tbid'].'">删除</button> '; 
                    }
              */
                
            }
            $echo['rows'][] = $tmp;
            unset($tmp);
        }
        echo json_encode($echo);
    }
    /**
     * 编辑或新增员工信息
     * @Author : bapijun
     * param 员工的信息
     * echo 编辑成功返回参数 y,失败返回  参数n
     */
    protected function edit()
    {
        global $db;
        $set = array(
            'family_role' => $_POST['family_role'],
            'city' => $_POST['city'],
            'address' => $_POST['address'],
            'child_name' => $_POST['child_name'],
            'child_age' => $_POST['child_age'],
            'child_sex' => $_POST['child_sex'],
        );
        
        if ($_POST['id'] != '') {
            $db->update('tb_member', $set, array(
                'tbid' => $_POST['id']
            ));
            $cb['status'] = 'y';
            
        } else {
            $cb['status'] = 'n';
        }
        echo json_encode($cb);
    }
    
    protected function delete()
    {
        global $db;
        $rs = $db->delete('tb_admin', array('tbid' => $_POST['id']));
        if($rs > 0){
            $cb['status'] = 'y';
        }else{
            $cb['status'] = 'n';
        }
        echo json_encode($cb);
    }

}

$ajax = new ajax();
$ajax->dealWithAjax();
